//
//  THMajorUserViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/22.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THMajorUserViewController.h"
#import "THBabyMajorUserTableViewCell.h"

@interface THMajorUserViewController ()
@property(nonatomic, strong) STTools_SegementAutoView                     *segView;/**<  */
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */
@end

@implementation THMajorUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.userModel.name;
    [self stoneobs_ui_configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{

     UIView * header = [UIView.alloc initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    __weak typeof(self) weakSelf =  self;
    self.segView = [[STTools_SegementAutoView alloc] initWithFrame:CGRectMake(20, 15, UISCREEN_WIDTH * 0.8, 44) andTitle:@[@"离我最近",@"星级最高",@"我的关注"] handle:^(STTools_SegementAutoView *sender, UIButton *currentSelctedButton) {
        
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    self.segView.lineSelectedColor = UIColor.clearColor;
    self.segView.lineSelectedView.backgroundColor = UIColor.clearColor;
    self.segView.lineColor = UIColor.clearColor;
    self.segView.autoMoveWithClic = YES;
    
    self.segView.butTitleSelectedColor = UIColor.blackColor;
    self.segView.butTitleColor = WH_UICOLOR_FROM_RGBA(0x343434);
    self.segView.butTitleFont = [UIFont systemFontOfSize:15];
    self.segView.butTitleSelectedFont = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:17];
    
    UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 21, 10)];
    stoneobs_ui_imageView.image = [UIImage imageNamed:@"椭圆4"];
    [self.segView.lineSelectedView addSubview:stoneobs_ui_imageView];
    stoneobs_ui_imageView.centerX = self.segView.lineSelectedView.width/2;
    stoneobs_ui_imageView.centerY =  self.segView.lineSelectedView.height / 2;
    self.segView.lineSelectedView.clipsToBounds = NO;
    self.segView.lineSelectedView.top = self.segView.lineSelectedView.top-5;
    self.segView.clipsToBounds = NO;
    self.segView.buttonBackGroundColor = UIColor.clearColor;
    self.segView.buttonSelctedBackGroundColor = UIColor.clearColor;
    self.segView.backgroundColor = UIColor.clearColor;
    [header addSubview:self.segView];
    header.height = self.segView.bottom + 10;
    self.tableView.tableHeaderView = header;
    
    self.tableView.backgroundColor = UIColor.whiteColor;

    self.tableView.st_noreslutView = [[STTools_NoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 30, STONEOBS_UI_UISCREEN_WIDTH, 400) title:@"暂无数据" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf stoneobs_fun_st_send_list_request];
    }];
    [self.tableView.mj_header beginRefreshing];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return THBabyMajorUserTableViewCell.stoneobs_ui_cell_height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    THBabyMajorUserTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[THBabyMajorUserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    THMajorUserModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    if (self.segView.cureentIndex == 1) {
        [cell showXingXingView];
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    THMajorUserModel * model = self.dataSouce[indexPath.row];
    model.major_name = self.title;
    THMajonDetailViewController * vc = THMajonDetailViewController.new;
    vc.userModel = model;
    [self pushViewController:vc animated:1];
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --stoneobs_net netWork Method
- (void)configRefreshFooter{
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf stoneobs_fun_st_send_list_request];
    }];
}
- (void)stoneobs_fun_st_send_list_request{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:@(self.page) forKey:@"page"];
    if (self.segView.cureentIndex == 0) {
        [stoneobs_net_param_dic setObject:@"nearby" forKey:@"sort"];
    }else if (self.segView.cureentIndex == 1){
        [stoneobs_net_param_dic setObject:@"star" forKey:@"sort"];
    }else{
        [stoneobs_net_param_dic setObject:@"1" forKey:@"get_follow"];
        
    }
    
    if (self.userModel.m_id.length) {
        [stoneobs_net_param_dic setObject:self.userModel.m_id forKey:@"major_id"];
        
    }
    //http://bf.xingzhuosong.com//v1/goods/goodsList?name=%E8%92%9C%E8%8B%97&page=1
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/nursery/nurseries"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
                CGFloat total = [[responseObject valueForKey:@"data"][@"total"] floatValue];
                CGFloat perPage = [[responseObject valueForKey:@"data"][@"perPage"] floatValue];;//每一页数据
                total = ceil(total/perPage);//总共多少页
        NSArray * array = [responseObject valueForKey:@"data"][@"items"];
        NSArray * objArry = [THMajorUserModel mj_objectArrayWithKeyValuesArray:array];
        if (self.page == 1) {
            self.dataSouce = NSMutableArray.new;
            [self.tableView.mj_header endRefreshing];
            if (total > self.page) {
                [self configRefreshFooter];
            }
        }else{
            if (total > self.page) {
                [self.tableView.mj_footer endRefreshing];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.dataSouce addObjectsFromArray:objArry];
        [self.tableView reloadData];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        if (self.page > 1) {
            self.page --;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
    
}
@end
